const db = require('../db')
const validationUtil = (schema, data) => {
    let {error, value} = schema.validate(data)  // error为ValidationError类型，value就是传入的data 
    if(error) {
        throw error // 验证不通过就抛出异常 
    }
}


// 查询条件处理为Sequelize模式 
const procWhere = function(where, schema) {
    console.log('where:', where)
    let Op = db.Op 
    let DataTypes = db.DataTypes 
    let andArr = [] 
    Object.keys(schema).forEach((key, idx) => {
        let attrSchema = schema[key] 
        let attrValue = where[key] 
        if(where.hasOwnProperty(key)) {
            // 普通string 
            if(attrSchema['type'] instanceof DataTypes.STRING && !attrSchema.select && !!attrValue) {
                andArr.push({
                    [key]: {
                        [Op.like]: `%${attrValue}%`
                    }
                })
            // 下拉选择string     
            } else if((attrSchema.type instanceof DataTypes.STRING && attrSchema.select && !!attrValue)
                        || attrSchema.type instanceof DataTypes.BOOLEAN) {
                andArr.push({
                    [key]: {
                        [Op.eq]: attrValue
                    }
                })
            // integer date，需要区间查询    
            } else if(attrSchema.type instanceof DataTypes.INTEGER || attrSchema.type instanceof DataTypes.DATEONLY) {
                if(attrValue.from === undefined || attrValue.from === null) { // from空
                    if(attrValue.to === undefined || attrValue.to === null) {
                        
                    } else {
                        andArr.push({
                            [key]: {
                                [Op.lte]: attrValue.to 
                            }
                        })
                    }
                } else { // from有 
                    if(attrValue.to === undefined || attrValue.from === null) {
                        andArr.push({
                            [key]: {
                                [Op.gte]: attrValue.from 
                            }
                        })
                    } else {
                        andArr.push({
                            [key]: {
                                [Op.between]: [attrValue.from, attrValue.to]
                            }
                        })
                    }
                }
              
            }
        }
    })
    let seqWhere = {
        [Op.and]: [ ...andArr
            /*
            {
                name: {
                    [Op.like]: '%hat'
                }
            },
            
            {
                credential_type: {
                    [Op.like]: '%hat'
                }
            },
            */
        ]
    } 
    console.log('seqWhere:', seqWhere)
    return seqWhere 
}

const procOrder = function(sort) {
    if(!sort || !sort.prop) {
        return null 
    } else {
        return [[sort.prop, sort.order]] 
    }
}


module.exports = {
    validationUtil,
    procWhere,
    procOrder
}
